package www.wmx.com.xssc.util.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import www.wmx.com.xssc.domain.doc.jcyjj.RbtKjjlJcyjjJbxx;
import www.wmx.com.xssc.domain.doc.jcyjj.RbtKjjlJcyjjWcry;
import www.wmx.com.xssc.domain.doc.jsfmj.RbtKjjlJsfmjJbxx;
import www.wmx.com.xssc.domain.doc.jsfmj.RbtKjjlJsfmjWcry;
import www.wmx.com.xssc.domain.doc.kjjbj.RbtKjjlKjjbjJbxx;
import www.wmx.com.xssc.domain.doc.kjjbj.RbtKjjlKjjbjWcry;
import www.wmx.com.xssc.domain.doc.yxntggj.RbtKjjlYxntggjZywcr;
import www.wmx.com.xssc.domain.mb.RbtKjjlMbTy;
import www.wmx.com.xssc.domain.vo.WcryShortXM;
import www.wmx.com.xssc.domain.vo.response.WcryResonse;
import www.wmx.com.xssc.service.jcyjj.IRbtKjjlJcyjjJbxxService;
import www.wmx.com.xssc.service.jcyjj.IRbtKjjlJcyjjWcryService;
import www.wmx.com.xssc.service.jsfmj.IRbtKjjlJsfmjJbxxService;
import www.wmx.com.xssc.service.jsfmj.IRbtKjjlJsfmjWcryService;
import www.wmx.com.xssc.service.kjjbj.IRbtKjjlKjjbjJbxxService;
import www.wmx.com.xssc.service.kjjbj.IRbtKjjlKjjbjWcryService;
import www.wmx.com.xssc.service.yxntggj.IRbtKjjlYxntggjJbxxService;
import www.wmx.com.xssc.service.yxntggj.IRbtKjjlYxntggjZywcrService;
import www.wmx.com.xssc.util.enumUtil.CgjlType;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import static www.wmx.com.xssc.util.time.DateFormatterUtil.StrToLocalDateTime;


/**
 * 主要是获取指定年度的所有申请奖项的所涉及的所有的完成人的基本信息：
 */
@Component
@Slf4j
public class WcrReadUtil {


    @Resource(description = "基础研究奖的基本信息表")
    private IRbtKjjlJcyjjJbxxService jcyjjJbxxService;
    @Resource(description = "基础研究奖完成人员")
    private IRbtKjjlJcyjjWcryService jcyjjWcryService;

    @Resource(description = "技术发明奖的基本信息表")
    private IRbtKjjlJsfmjJbxxService jsfmjJbxxService;
    @Resource(description = "技术发明奖完成人员")
    private IRbtKjjlJsfmjWcryService jsfmjWcryService;

    @Resource(description = "科技进步奖的基本信息表")
    private IRbtKjjlKjjbjJbxxService kjjlKjjbjJbxxService;
    @Resource(description = "科技进步奖完成人员")
    private IRbtKjjlKjjbjWcryService kjjlKjjbjWcryService;

    @Resource(description = "一线难题攻关奖基本信息表")
    private IRbtKjjlYxntggjJbxxService yxntggjJbxxService;
    @Resource(description = "一线难题攻关奖完成人员")
    private IRbtKjjlYxntggjZywcrService yxntggjZywcrService;


    public WcryResonse checkWcry(List<RbtKjjlMbTy> mbTyList, int type, WcryShortXM wcry) {
        WcryResonse wcryResonse = new WcryResonse();
        RbtKjjlMbTy rbtKjjlMbTy = mbTyList.get(type);
        List<Integer> containList = rbtKjjlMbTy.getContainList();
        List<RbtKjjlJcyjjWcry> otherJCYJJXM = new ArrayList<>();
        List<RbtKjjlJsfmjWcry> otherJSFMJXM = new ArrayList<>();
        List<RbtKjjlKjjbjWcry> otherKJJBJXM = new ArrayList<>();

        if (containList.contains(CgjlType.JCYJJ.getCode())) {
            List<RbtKjjlJcyjjWcry> list = jcyjjWcryService.lambdaQuery()
                    .ne(RbtKjjlJcyjjWcry::getKjjlJcyjjId, wcry.getJbxxID())
                    .eq(RbtKjjlJcyjjWcry::getXm, wcry.getXm())
                    .eq(RbtKjjlJcyjjWcry::getDwid, wcry.getDwid())
                    .list();
            if (list != null && !list.isEmpty()) {
                otherJCYJJXM.addAll(list);
            }
        } else if (containList.contains(CgjlType.JSFMJ.getCode())) {
            List<RbtKjjlJsfmjWcry> list = jsfmjWcryService.lambdaQuery()
                    .ne(RbtKjjlJsfmjWcry::getKjjlJsfmjId, wcry.getJbxxID())
                    .eq(RbtKjjlJsfmjWcry::getXm, wcry.getXm())
                    .eq(RbtKjjlJsfmjWcry::getDwid, wcry.getDwid())
                    .list();
            if (list != null && !list.isEmpty()) {
                otherJSFMJXM.addAll(list);
            }
        } else if (containList.contains(CgjlType.KJJBJ.getCode())) {
            List<RbtKjjlKjjbjWcry> list = kjjlKjjbjWcryService.lambdaQuery()
                    .ne(RbtKjjlKjjbjWcry::getKjjlKjjbjId, wcry.getJbxxID())
                    .eq(RbtKjjlKjjbjWcry::getXm, wcry.getXm())
                    .eq(RbtKjjlKjjbjWcry::getDwid, wcry.getDwid())
                    .list();
            if (list != null && !list.isEmpty()) {
                otherKJJBJXM.addAll(list);
            }
        }
        wcryResonse.setOtherJCYJJXM(otherJCYJJXM);
        wcryResonse.setOtherJSFMJXM(otherJSFMJXM);
        wcryResonse.setOtherKJJBJXM(otherKJJBJXM);
        int sum;
        sum = otherJCYJJXM.size() + otherJSFMJXM.size() + otherKJJBJXM.size();
        if (sum > rbtKjjlMbTy.getMaxNum()) {
            wcryResonse.setSuccess(false);
            wcryResonse.setStatus(2);
            wcryResonse.getFailReason().add("超过最大推荐数");
        } else {
            wcryResonse.setSuccess(true);
        }
        return wcryResonse;
    }


    public List<WcryShortXM> getWcryFilterByTypeAndId(int cgjlType, String jbxxID) {
        List<WcryShortXM> result = new ArrayList<>();
        if (cgjlType == CgjlType.JCYJJ.getCode()) {
            List<RbtKjjlJcyjjWcry> list = jcyjjWcryService.lambdaQuery()
                    .eq(RbtKjjlJcyjjWcry::getKjjlJcyjjId, jbxxID)
                    .list();
            for (RbtKjjlJcyjjWcry wcry : list) {
                WcryShortXM shortXM = WcryShortXM.builder()
                        .xm(wcry.getXm())
                        .xb(wcry.getXb())
                        .dwid(wcry.getDwid())
                        .zc(wcry.getZc())
                        .zc(wcry.getZc())
                        .cjsjq(wcry.getCjsjq())
                        .cjsjz(wcry.getCjsjz())
                        .build();
                result.add(shortXM);
            }

        } else if (cgjlType == CgjlType.JSFMJ.getCode()) {

            List<RbtKjjlJsfmjWcry> list = jsfmjWcryService.lambdaQuery()
                    .eq(RbtKjjlJsfmjWcry::getKjjlJsfmjId, jbxxID)
                    .list();
            for (RbtKjjlJsfmjWcry wcry : list) {
                WcryShortXM shortXM = WcryShortXM.builder()
                        .xm(wcry.getXm())
                        .xb(wcry.getXb())
                        .dwid(wcry.getDwid())
                        .zc(wcry.getZc())
                        .zc(wcry.getZc())
                        .cjsjq(wcry.getCjsjq())
                        .cjsjz(wcry.getCjsjz())
                        .build();
                result.add(shortXM);
            }

        } else if (cgjlType == CgjlType.KJJBJ.getCode()) {

            List<RbtKjjlKjjbjWcry> list = kjjlKjjbjWcryService.lambdaQuery()
                    .eq(RbtKjjlKjjbjWcry::getKjjlKjjbjId, jbxxID)
                    .list();
            for (RbtKjjlKjjbjWcry wcry : list) {
                WcryShortXM shortXM = WcryShortXM.builder()
                        .xm(wcry.getXm())
                        .xb(wcry.getXb())
                        .dwid(wcry.getDwid())
                        .zc(wcry.getZc())
                        .zc(wcry.getZc())
                        .cjsjq(wcry.getCjsjq())
                        .cjsjz(wcry.getCjsjz())
                        .build();
                result.add(shortXM);
            }

        }else if(cgjlType == CgjlType.YXNTGGJ.getCode()){

            List<RbtKjjlYxntggjZywcr> list = yxntggjZywcrService.lambdaQuery()
                    .eq(RbtKjjlYxntggjZywcr::getKjjlYxntggjId, jbxxID)
                    .list();
            for (RbtKjjlYxntggjZywcr wcry : list) {
                WcryShortXM shortXM = WcryShortXM.builder()
                        .xm(wcry.getXm())
                        .xb(wcry.getXb())
                        .dwid(wcry.getDwid())
                        .zc(wcry.getZc())
                        .zc(wcry.getZc())
                        .build();
                result.add(shortXM);
            }
        }
        return null;
    }

    public List<WcryShortXM> initWcryProjectXM(String nd) {

        List<WcryShortXM> wcryShortXMList = new ArrayList<>();

        //1.基础研究奖：
        List<RbtKjjlJcyjjJbxx> jcyjjJbxxes = jcyjjJbxxService.lambdaQuery()
                .eq(RbtKjjlJcyjjJbxx::getNd, nd)
                .list();
        for (RbtKjjlJcyjjJbxx jcyjjJbxx : jcyjjJbxxes) {
            String xmzqq = jcyjjJbxx.getXmzqq();
            LocalDateTime xmBegin = null;
            LocalDateTime xmEnd = null;
            if (xmzqq != null) {
                xmBegin = StrToLocalDateTime(xmzqq);
            }
            String xmzqz = jcyjjJbxx.getXmzqz();
            if (xmzqz != null) {
                xmEnd = StrToLocalDateTime(xmzqz);
            }
            String jcyjjId = jcyjjJbxx.getId();
            List<RbtKjjlJcyjjWcry> jcyjjWcryList = jcyjjWcryService.lambdaQuery()
                    .eq(RbtKjjlJcyjjWcry::getKjjlJcyjjId, jcyjjId)
                    .list();
            for (RbtKjjlJcyjjWcry jcyjjWcry : jcyjjWcryList) {
                //项目的基本信息：
                WcryShortXM wcryShortXM = new WcryShortXM();
                wcryShortXM.setNd(nd);
                wcryShortXM.setXmmc(jcyjjJbxx.getBjmc());
                wcryShortXM.setXmBeginTime(xmBegin);
                wcryShortXM.setXmEndTime(xmEnd);
                wcryShortXM.setCgjlType(CgjlType.JCYJJ.getCode());
                wcryShortXM.setJbxxID(jcyjjId);
                //完成人员的基本信息：
                wcryShortXM.setXm(jcyjjWcry.getXm());
                wcryShortXM.setXb(jcyjjWcry.getXb());
                wcryShortXM.setDwid(jcyjjWcry.getDwid());
                wcryShortXM.setZw(jcyjjWcry.getZw());
                wcryShortXM.setZc(jcyjjWcry.getZc());
                wcryShortXM.setCjsjq(jcyjjWcry.getCjsjq());
                wcryShortXM.setCjsjz(jcyjjWcry.getCjsjz());
                wcryShortXMList.add(wcryShortXM);
            }
        }
        //2.技术发明奖
        List<RbtKjjlJsfmjJbxx> jsfmjJbxxList = jsfmjJbxxService.lambdaQuery()
                .eq(RbtKjjlJsfmjJbxx::getNd, nd)
                .list();
        for (RbtKjjlJsfmjJbxx jsfmjJbxx : jsfmjJbxxList) {
            String xmzqq = jsfmjJbxx.getXmzqq();
            String xmzqz = jsfmjJbxx.getXmzqz();
            LocalDateTime xmBegin = null;
            LocalDateTime xmEnd = null;
            if (xmzqq != null) {
                xmBegin = StrToLocalDateTime(xmzqq);
            }
            if (xmzqz != null) {
                xmEnd = StrToLocalDateTime(xmzqz);
            }
            String jsfmjId = jsfmjJbxx.getId();
            List<RbtKjjlJsfmjWcry> jsfmjWcryList = jsfmjWcryService.lambdaQuery()
                    .eq(RbtKjjlJsfmjWcry::getKjjlJsfmjId, jsfmjId)
                    .list();
            for (RbtKjjlJsfmjWcry jsfmjWcry : jsfmjWcryList) {
                //项目的基本信息：
                WcryShortXM wcryShortXM = new WcryShortXM();
                wcryShortXM.setNd(nd);
                wcryShortXM.setXmmc(jsfmjJbxx.getXmmc());
                wcryShortXM.setXmBeginTime(xmBegin);
                wcryShortXM.setXmEndTime(xmEnd);
                wcryShortXM.setCgjlType(CgjlType.JSFMJ.getCode());
                wcryShortXM.setJbxxID(jsfmjId);
                //完成人员的基本信息：
                wcryShortXM.setXm(jsfmjWcry.getXm());
                wcryShortXM.setXb(jsfmjWcry.getXb());
                wcryShortXM.setDwid(jsfmjWcry.getDwid());
                wcryShortXM.setZw(jsfmjWcry.getZw());
                wcryShortXM.setZc(jsfmjWcry.getZc());
                wcryShortXM.setCjsjq(jsfmjWcry.getCjsjq());
                wcryShortXM.setCjsjz(jsfmjWcry.getCjsjz());
                wcryShortXMList.add(wcryShortXM);
            }
        }
        //3.科技进步奖：
        List<RbtKjjlKjjbjJbxx> kjjlKjjbjJbxxesList = kjjlKjjbjJbxxService.lambdaQuery()
                .eq(RbtKjjlKjjbjJbxx::getNd, nd)
                .list();
        for (RbtKjjlKjjbjJbxx kjjlKjjbjJbxx : kjjlKjjbjJbxxesList) {

            String jsfmjId = kjjlKjjbjJbxx.getId();
            String xmzqq = kjjlKjjbjJbxx.getXmzqq();
            String xmzqz = kjjlKjjbjJbxx.getXmzqz();
            LocalDateTime xmBegin = null;
            LocalDateTime xmEnd = null;
            if (xmzqq != null) {
                xmBegin = StrToLocalDateTime(xmzqq);
            }
            if (xmzqz != null) {
                xmEnd = StrToLocalDateTime(xmzqz);
            }
            List<RbtKjjlKjjbjWcry> jsfmjWcryList = kjjlKjjbjWcryService.lambdaQuery()
                    .eq(RbtKjjlKjjbjWcry::getKjjlKjjbjId, jsfmjId)
                    .list();
            for (RbtKjjlKjjbjWcry kjjlKjjbjWcry : jsfmjWcryList) {
                //项目的基本信息：
                WcryShortXM wcryShortXM = new WcryShortXM();
                wcryShortXM.setNd(nd);
                wcryShortXM.setXmmc(kjjlKjjbjJbxx.getXmmc());
                wcryShortXM.setXmBeginTime(xmBegin);
                wcryShortXM.setXmEndTime(xmEnd);
                wcryShortXM.setCgjlType(CgjlType.KJJBJ.getCode());
                wcryShortXM.setJbxxID(jsfmjId);
                //完成人员的基本信息：
                wcryShortXM.setXm(kjjlKjjbjWcry.getXm());
                wcryShortXM.setXb(kjjlKjjbjWcry.getXb());
                wcryShortXM.setDwid(kjjlKjjbjWcry.getDwid());
                wcryShortXM.setZw(kjjlKjjbjWcry.getZw());
                wcryShortXM.setZc(kjjlKjjbjWcry.getZc());
                wcryShortXM.setCjsjq(kjjlKjjbjWcry.getCjsjq());
                wcryShortXM.setCjsjz(kjjlKjjbjWcry.getCjsjz());
                wcryShortXMList.add(wcryShortXM);
            }
        }
        return wcryShortXMList;
    }


}
